/* -----------------
* brief
* 1. this is code gen by tools
* 2. for add/update/delete/query the data save in sql database
 */

package user

import (
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/utils"
	"saas_sys_base/common"
	"saas_sys_base/redishelper"

	//"github.com/mjiulee/lego/utils"
)


const REDIS_USER_REL_BY_ID_PREFIX string = "redis_user_rel_by_id_"
const REDIS_USER_REL_BY_TID_Uid_PREFIX string = "redis_user_rel_by_tid_uid_"

/* 根据id获取记录
 * -----------------
 */
func (t *AppTenantUserRel) RecodeById(id int64) *AppTenantUserRel {
	item := new(AppTenantUserRel)

	err := redishelper.RedisGetKeyEx(REDIS_USER_REL_BY_ID_PREFIX+utils.Int64ToString(id), item)
	if err == nil {
		return item
	}

	_, err = lego.GetDBEngine().ID(id).Get(item)
	if item.Id <= 0 || err != nil {
		return nil
	}

	_ = redishelper.RedisSetKeyEx(REDIS_USER_REL_BY_ID_PREFIX+utils.Int64ToString(id), item)

	return item
}


func (t *AppTenantUserRel) RecodeByWho(who common.Who /*MiniAppId int64, tid int64, Uid int64*/) *AppTenantUserRel {
	item := new(AppTenantUserRel)
	err := redishelper.RedisGetKeyEx(REDIS_USER_REL_BY_TID_Uid_PREFIX+who.ToString(), item)
	if err == nil {
		return item
	}

	//lego.GetDBEngine().Where("mini_app_id = ? AND tid = ? AND sys_uid = ?", MiniAppId, tid, Uid).Get(item)
	who.GetDBEngineWithFilter().Get(item)
	if item.Id <= 0 {
		return nil
	}

	_ = redishelper.RedisSetKeyEx(REDIS_USER_REL_BY_TID_Uid_PREFIX+who.ToString(), item)
	return item
}

/* 添加
 * -----------------
 */
func (t *AppTenantUserRel) AddRecode(item2add *AppTenantUserRel) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}

	_ = redishelper.RedisDelKey(REDIS_USER_REL_BY_ID_PREFIX+utils.Int64ToString(item2add.Id))
	_ = redishelper.RedisDelKey(REDIS_USER_REL_BY_TID_Uid_PREFIX+common.NewWho(item2add.TenantId, item2add.MiniAppId, item2add.Uid).ToString())

	return true
}

func (t *AppTenantUserRel) AddRecodeWithsession(session *xorm.Session, item2add *AppTenantUserRel) bool {
	item2add.Id = lego.UUID()
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}

	_ = redishelper.RedisDelKey(REDIS_USER_REL_BY_ID_PREFIX+utils.Int64ToString(item2add.Id))
	_ = redishelper.RedisDelKey(REDIS_USER_REL_BY_TID_Uid_PREFIX+common.NewWho(item2add.TenantId, item2add.MiniAppId, item2add.Uid).ToString())

	return true
}

/* 删除(硬删除)*/
func (t *AppTenantUserRel) DelRecodeById(id int64) bool {
	item := new(AppTenantUserRel)
	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}

	_ = redishelper.RedisDelKey(REDIS_USER_REL_BY_ID_PREFIX+utils.Int64ToString(item.Id))
	_ = redishelper.RedisDelKey(REDIS_USER_REL_BY_TID_Uid_PREFIX+common.NewWho(item.TenantId, item.MiniAppId, item.Uid).ToString())

	return true
}

func (t *AppTenantUserRel) DelRecodeByIdWithSession(session *xorm.Session, id int64) bool {
	item := new(AppTenantUserRel)
	_, err := session.ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}

	_ = redishelper.RedisDelKey(REDIS_USER_REL_BY_ID_PREFIX+utils.Int64ToString(item.Id))
	_ = redishelper.RedisDelKey(REDIS_USER_REL_BY_TID_Uid_PREFIX+common.NewWho(item.TenantId, item.MiniAppId, item.Uid).ToString())

	return true
}

/* 更新
 * -----------------*/
func (t *AppTenantUserRel) UpdateRecode(rc *AppTenantUserRel) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}

	_ = redishelper.RedisDelKey(REDIS_USER_REL_BY_ID_PREFIX+utils.Int64ToString(rc.Id))
	_ = redishelper.RedisDelKey(REDIS_USER_REL_BY_TID_Uid_PREFIX+common.NewWho(rc.TenantId, rc.MiniAppId, rc.Uid).ToString())

	return true
}

func (t *AppTenantUserRel) UpdateRecodeWithSession(session *xorm.Session, rc *AppTenantUserRel) bool {
	_, err := session.ID(rc.Id).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}

	_ = redishelper.RedisDelKey(REDIS_USER_REL_BY_ID_PREFIX+utils.Int64ToString(rc.Id))

	// TODO 这里使用who，但update的数据有可能who为0
	_ = redishelper.RedisDelKey(REDIS_USER_REL_BY_TID_Uid_PREFIX+common.NewWho(rc.TenantId, rc.MiniAppId, rc.Uid).ToString())

	return true
}
